<template>
<div class="big-box">
  <my-header titleName="购物车"></my-header>
  <div class="cart">
      <div class="empty-cart" v-if="cartList.length===0">
        <img src="" alt="" class="empty-cart-img">
        <div class="empty-cart-text">购物车还是空的</div>
        <div class="btn">去逛逛</div>
      </div>
      <div class="cart-container" v-else v-for="(item,index) in cartList" :key="index">
        <span class="iconfont toogle" :class="item.selectState?'icon-checkbox-marked-circ':'icon-checkbox-blank-circle-outline'" @click="toggleCheck(index)"></span>
        <div class="item-detail">
          <img :src="item.img" :alt="item.title">
          <div class="item-info">
            <h3>{{item.title}}</h3>
            <div class="pay">
              <div class="pay-price">￥{{item.price}}</div>
              <div class="edit-quantity">
                <p class="operate-btn iconfont icon-minus" @click="handleReduce(index)"></p>
                <p class="btn-input">{{item.num}}</p>
                <p class="operate-btn iconfont icon-plus" @click="handleAdd(index)"></p>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="cart-footer">
        <div class="cart-footer-left"><span class="iconfont toogle" :class="isAllSelect?'icon-checkbox-marked-circ':'icon-checkbox-blank-circle-outline'" @click="changeAllSelect"></span><span>全选</span></div>
        <div class="cart-footer-center"><span>合计：</span><span class="total-price">￥{{totalPrice}}</span></div>
        <div class="cart-footer-right"><span class="goto-pay">结算({{totalNun}})</span></div>
      </div>
      <!-- 弹框是否删除商品 -->
      <confirm v-model="confirmShow" @on-confirm="onConfirm" content="确定删除该商品？"></confirm>
    </div>
  </div>

</template>

<script>
import myHeader from '@/components/myHeader'
import { Confirm } from 'vux'
export default {
  components: {
    myHeader,
    Confirm
  },
  data () {
    return {
      cartList: [],
      confirmShow: false,
      index: 0

    }
  },
  created () {
    // let locaData = [{ id: 5, selectState: true, img: '555', title: '呜呜呜', price: 555, num: 5 },
    //   { id: 6, selectState: true, img: '666', title: '溜溜溜', price: 666, num: 6 }]
    // 读取本地存储的购物车数据
    this.cartList = JSON.parse(localStorage.getItem('cartList')) || []
    console.log(this.cartList)
  },
  beforeDestroy () {
    // 在销毁阶段中将数据写入到本地保存
    localStorage.setItem('cartList', JSON.stringify(this.cartList))
  },
  methods: {
    // 小选项
    toggleCheck (index) {
      this.cartList[index].selectState = !this.cartList[index].selectState
    },
    // 总选项
    changeAllSelect () {
      let flag = !this.isAllSelect
      this.cartList.forEach(item => {
        item.selectState = flag
      })
    },
    // 增加
    handleAdd (index) {
      this.cartList[index].num++
    },
    // 减少
    handleReduce (index) {
      // 判断是否是最后一个
      if (this.cartList[index].num === 1) {
        this.confirmShow = true
        this.index = index
      } else {
        this.cartList[index].num--
      }
    },
    // 点击确认删除按钮后
    onConfirm (msg) {
      if (!msg) {
        console.log('123')
        // 删除该对象
        this.cartList.splice(this.index, 1)
      }
    }

  },
  computed: {
    // 总数
    totalNun () {
      let num = 0
      this.cartList.forEach(item => {
        // 判断是否被选中
        if (item.selectState) {
          num += item.num
        }
      })
      return num
    },
    // 总价
    totalPrice () {
      let num = 0
      this.cartList.forEach(item => {
        if (item.selectState) {
          num += item.num * item.price
        }
      })
      return num
    },
    // 监听是否全选
    isAllSelect () {
      let flag = true
      this.cartList.forEach(item => {
        // 如果有一个
        if (!item.selectState) {
          flag = false
        }
      })
      return flag
    }
  }
}
</script>

<style lang="less" scoped>
.big-box{
  height: 100%;
}
.cart {
  height: auto;
  margin-top: 45px;
  margin-bottom: 50px;
  font-size: 14px;
  .empty-cart {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
    .empty-cart-img {
      height: 90px;
      width: 90px;
    }
    .empty-cart-text {
      font-size: 20px;
      color: #999;
      padding: 15px 0;
    }
    .btn {
      font-size: 20px;
      padding: 15px 55px;
      text-align: center;
      margin: 0 auto;
      border-radius: 10px;
      background: #ed601b;
      color: #fff;
    }
  }
  .cart-container {
    display: flex;
    align-items: center;
    margin: 5px;
    padding: 8px 10px;
    border: 1px solid #eeeeee;
    border-radius: 20px;
    .toogle {
      width: 20px;
      height: 20px;
      line-height: 20px;
      font-size: 20px;
    }
    .icon-checkbox-marked-circ {
      color: #ff5500;
    }
    .icon-checkbox-blank-circle-outline {
      color: #666666;
    }
  }
  .item-detail {
    display: flex;
    flex: 1;
    img {
      margin: 2px 0 2px 5px;
      width: 90px;
      height: 90px;
    }
    .item-info {
      width: 100%;
      padding: 5px 10px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      h3 {
        font-size: 16px;
        font-weight: 400;
      }
      .pay {
        display: flex;
        justify-content: space-between;
        .pay-price {
          color: #ff5500;
          font-weight: bold;
          line-height: 30px;
        }
        .edit-quantity {
          display: flex;
          height: 30px;
          line-height: 30px;
          .operate-btn {
            padding: 0 10px;
            font-size: 20px;
            color: #bababa;
          }
          .btn-minus {
            font-size: 14px;
          }
        }
      }
    }
  }
  .cart-footer {
    display: flex;
    justify-content: space-between;
    position: fixed;
    bottom: 0;
    left: 0;
    height: 50px;
    line-height: 50px;
    width: 100%;
    border-top: 1px solid #e7e7e7;
    background-color: #fff;
    .cart-footer-left {
      display: flex;
      justify-content: cneter;
      align-items: center;
      span {
        display: block;
        height: 50px;
        padding: 0 5px;
      }
    }
    .cart-footer-center {
      .total-price {
        color: #ff5500;
        font-weight: bold;
      }
    }
    .cart-footer-right {
      display: flex;
      flex-direction: column;
      padding: 0 20px;
      background-color: #ff5500;
      .goto-pay {
        color: #fff;
      }
    }
    .icon-checkbox-marked-circ {
      color: #ff5500;
    }
    .icon-checkbox-blank-circle-outline {
      color: #666666;
    }
  }
}
</style>
